package cn.stronglink.asset.terminal.cq.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import cn.stronglink.asset.model.InventorySheet;
import cn.stronglink.asset.model.InventorySheetDetail;
import cn.stronglink.asset.module.inventory.mapper.InventoryMapper;
import cn.stronglink.asset.terminal.cq.vo.InventorySheetDetailVo;
import cn.stronglink.asset.terminal.cq.vo.InventorySheetVo;
import cn.stronglink.asset.terminal.cq.vo.UploadInventoryParam;

@Service
@Transactional(rollbackFor=Exception.class) 
public class CqService {
	@Autowired
	private InventoryMapper inventoryMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<InventorySheetVo> qryInventorySheetList() {
		List<InventorySheetVo> list =inventoryMapper.qryInventorySheetList();
		if(list!=null&&list.size()>0) {
			for(InventorySheetVo sheet:list) {
				List<InventorySheetDetailVo> detailList=inventoryMapper.qryInventorySheetDetail(Long.parseLong(sheet.getInventorySheetId_()));
				sheet.setListData(detailList);
			}
		}
		return list;
	}

	public void uploadInventory(List<UploadInventoryParam> paramList) {
		Set<Long> sheetIdList = new HashSet<Long>();
		for(UploadInventoryParam obj:paramList) {
			sheetIdList.add(obj.getInventorySheetId());
		}
		
		if(sheetIdList!=null&&sheetIdList.size()>0) {
			List<InventorySheet> sheetList = new ArrayList<InventorySheet>();
			for(Long sheetId:sheetIdList) {
				InventorySheet sheet = new InventorySheet();
				sheet.setId(sheetId);
				sheet.setStatus(2);
				sheet.setExecuteTime(new Date());
				sheet.setUpdateTime(new Date());
				sheet.setCreateBy(879224697238376448l);
				sheetList.add(sheet);
			}
			//批量更新盘点单为已完成
			HashMap<String,Object> map = new HashMap<String,Object>();
			map.put("sheetList", sheetList);
			inventoryMapper.updateInventorySheetBatch(map);
			
			
			//先将盘点单里的盘点详情都更新成盘亏状态
			HashMap<String,Object> detailmap = new HashMap<String,Object>();
			detailmap.put("sheetIdList", sheetIdList);
			inventoryMapper.uploadDetailBySheetId(detailmap);
			
		}
		
		for(UploadInventoryParam param:paramList) {
			InventorySheetDetail detail = new InventorySheetDetail();
			if(!"".equals(param.getInventorySheetDetailId())&&param.getInventorySheetDetailId()!=null&&!"".equals(param.getCode900M())&&param.getCode900M()!=null) {
				//判断如果当前设备的900M标签对不上当前的设备，则是差异
				InventorySheetDetail d=inventoryMapper.qryDetailById(param.getInventorySheetDetailId());
				detail.setId(Long.parseLong(param.getInventorySheetDetailId()));
				detail.setStatus(2);//差异
				detail.setUpdateBy(879224697238376448l);
				detail.setUpdateTime(new Date());
				if(d!=null){
					if(!"".equals(d.getRfidNo())&&d.getRfidNo()!=null) {
						if(d.getRfidNo().equals(param.getCode900M())||d.getRfidNo()==param.getCode900M()) {
							detail.setStatus(3);//正常
						}
					}
					inventoryMapper.uploadInventorySheetDetail(detail);
				}
				
			}else {
				//盘盈
				detail.setId(IdWorker.getId());
				detail.setInventorySheetId(param.getInventorySheetId());
				detail.setRfidNo(param.getCode900M());
				detail.setIsHandle(2);
				detail.setStatus(5);
				detail.setCreateBy(879224697238376448l);
				detail.setCreateTime(new Date());
				inventoryMapper.insertInventorySheetDetailOne(detail);
			}
		}
		
	}
}
